<?php

class SearchEngine{
	private $options;
	private $uselessWords;
	
	/**
	 * Retourne vrai si $aWord est contenu dans $anExpression en utilisant le wordCompare
	 */
	 public function containsWord($aWord,$anExpression){
	 	//A retirer
	 	$anExpression=$this->cleanExpression($anExpression);
		$words=explode(" ", $anExpression);
		$trouve=false;
		$count=count($words);
		$i=0;
		while($trouve!==TRUE && $i<$count){
			$trouve=$this->wordCompare($aWord, $words[$i]);
			$i++;
		}
		return $trouve;
	}
	
	public function __construct(){
		$this->options=array("libelle"=>true,"useCase"=>true,"systemMessage"=>true,"userAction"=>true,"description"=>true,"expressionComp"=>1/2,"wordComp"=>75);
		$this->uselessWords=array("et","avec","d","le","la","les","l","un","une","de","des","mais","ou","et","donc","or","ni","car","se","te","qu","que","qui","sur","dans");
	}
	
	/**
	 * Nettoyage d'un mot avant comparaison à un autre
	 * Suppression des doubles lettres consécutives, des pluriels, des traits d'union
	 */
	public function cleanWord($aWord){
		$aWord = htmlentities($aWord, ENT_NOQUOTES,'utf-8');
		
		$aWord = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $aWord); //supprime les accents
		$aWord = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $aWord); // pour les ligatures e.g. '&oelig;'
		$aWord = preg_replace('#&[^;]+;#', '', $aWord); // supprime les autres caractères
		
		
		$aWord = strtolower($aWord); // Majuscule => minuscule
		
		$aWord=preg_replace("/(\w{2,})s/u","$1",$aWord); // Pluriels => singulier
		$aWord=preg_replace("<(.)\\1+(?!\\1)>", "$1", $aWord); // Double lettre => une lettre
		
		return $aWord;
	}
	
	/**
	 * Suppression des mots inutiles
	 * @param string $anExpression
	 */
	public function cleanExpression($anExpression){
		$anExpression=preg_replace("/\s|[\'\-\,\.\;\?\!\:]/", "#espace#", $anExpression);
		$anExpression=preg_replace("<(#espace#)\\1+(?!\\1)>", "$1", $anExpression); // Double #espace# => un #espace#
		$words=explode("#espace#", $anExpression);
		$wordsCorr=array();
		foreach ($words as $word){
			$word=$this->cleanWord($word);
			$wordsCorr[]=$word;
		}
		$words=array_diff($wordsCorr, $this->uselessWords);
		return implode(" ", $words);
	}
	
	/**
	 * 
	 * @param string $word1
	 * @param string $word2
	 * @return boolean vrai si les 2 mots sont identiques
	 */
	public function wordCompare($word1,$word2){
		$word1=$this->cleanWord($word1);
		$word2=$this->cleanWord($word2);
		similar_text($word1, $word2,$percent);
		return number_format($percent,0)>=$this->options["wordComp"];
		
	}
	
	/**
	 *
	 * @param string $expression1
	 * @param string $expression2
	 * @return boolean vrai si les 2 expressions sont identiques
	 */
	public function expressionCompare($expression1,$expression2){
		$expression1=$this->cleanExpression($expression1);
		$expression2=$this->cleanExpression($expression2);
		$words=explode(" ", $expression1);
		$count=count($words);
		$countEquals=0;
		$equals=false;
		$i=0;
		while($equals!==TRUE && $i<$count){
			if($this->containsWord($words[$i], $expression2))
				$countEquals++;
			$equals=(($countEquals/$count)>=$this->options["expressionComp"]);
			$i++;
			//echo $this->options["expressionComp"];
		}
		return $equals;
	}
	
	
	
	public function compare(Report $report1,Report $report2){
	
		if($report2->getLibelle()==""){
			return true;
		}
		elseif(($this->options['libelle'])&&(!$this->expressionCompare($report1->getLibelle(), $report2->getLibelle()))){
			return FALSE;
		}
		
	/*	if(($this->options['useCase'])&&($report1->getCode()!=$report2->getCode())){
			return FALSE;
		}*/
		
				
		if($report2->getDescription()==""){
			return true;
		}
		elseif(($this->options['description'])&&(!$this->expressionCompare($report1->getDescription(), $report2->getDescription()))){
			return FALSE;
		}
		
		if($report2->getUserAction()==""){
			return true;
		}
		if(($this->options['userAction'])&&(!$this->expressionCompare($report1->getUserAction(), $report2->getUserAction()))){
			return FALSE;
		}
		
		if($report2->getSystemMessage()==""){
			echo "e";
			return true;
		}
		elseif(($this->options['systemMessage'])&&(!$this->expressionCompare($report1->getSystemMessage(), $report2->getSystemMessage()))){
			return FALSE;
		}
		
		return true;
	}
	
	public function addUselessWord($aWord){
		if(!in_array($aWord, $this->uselessWords))
			$this->uselessWords[]=$aWord;
	}
	
	public function setOption($key,$value){
		$this->options[$key]=$value;
	}

}